﻿using SupermarkedModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Supermarket.Client
{
    internal class XMLReportGenerator
    {
        public static void GenerateSalesReport()
        {
            SupermarketDbEntities entities = new SupermarketDbEntities();

            string fileName = "../../Aggregated Sales Reports/Sales-by-Vendors-report.xml";
            Encoding encoding = Encoding.GetEncoding("utf-8");

            using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("sales");

                foreach (Vendor vendor in entities.Vendors)
                {
                    var incomesByDate = from v in entities.Vendors
                                        join p in entities.Products
                                        on v.VendorID equals p.VendorID
                                        join r in entities.Reports
                                        on p.ID equals r.ProductID
                                        where v.VendorID == vendor.VendorID
                                        select new { Date = r.Date, Sum = r.Sum };

                    var aggregatedIncomeByDate = (from i in incomesByDate
                                                  group i by i.Date into g
                                                  select new IncomeByDateDisplay { Date = g.Key, Sum = g.Sum(i => i.Sum) }).ToList();

                    WriteSale(writer, vendor.VendorName, aggregatedIncomeByDate);

                }
                writer.WriteEndDocument();
            }
        }

        private static void WriteSale(XmlWriter writer, string vendor, List<IncomeByDateDisplay> incomesByDate)
        {
            writer.WriteStartElement("sale");
            writer.WriteAttributeString("vendor", vendor);
            foreach (var income in incomesByDate)
            {
                WriteSummary(writer, income);
            }
            writer.WriteEndElement();
        }

        private static void WriteSummary(XmlWriter writer, IncomeByDateDisplay incomeByDate)
        {
            writer.WriteStartElement("summary");
            writer.WriteAttributeString("date", incomeByDate.Date.ToString("yyyy-mm-dd"));
            writer.WriteAttributeString("total-sum", incomeByDate.Sum.ToString());
            writer.WriteEndElement();
        }
    }

    // CREATE PROC usp_FindVendorIncomeByDate (@vendorId int) 
    // AS
    //    SELECT SUM(r.Sum), r.Date
    //    FROM Vendors v JOIN Products p 
    //    ON v.VendorID = p.VendorID
    //    JOIN Reports r
    //    ON r.ProductID = p.ID
    //    WHERE v.VendorId = @vendorId
    //    GROUP BY r.Date
    // GO

    public class IncomeByDateDisplay
    {
        public DateTime Date { get; set; }
        public decimal Sum { get; set; }
    }
}
